<template>
  <el-menu
    default-active="/home"
    background-color="#23262e"
    text-color="#fff"
    class="el-menu-vertical-demo"
    active-text-color="#409EFF"
    :collapse="showNavbar"
    router
    unique-opened="true"
  >
    <!-- 侧边栏导航区域 -->

    <template v-for="item in menus">
      <!-- 不包含子菜单的“一级菜单” -->
      <!-- unique-opened:默认展开 -->
      <el-menu-item
        :index="'/layout' + item.indexPath"
        :key="item.indexPath"
        v-if="!item.children"
      >
        <i :class="item.icon"></i>
        <span slot="title">{{ item.title }}</span>
      </el-menu-item>
      <!-- 包含子菜单的“一级菜单” -->
      <el-submenu :index="item.indexPath" :key="item.indexPath" v-else>
        <template slot="title">
          <i :class="item.icon"></i>
          <span>{{ item.title }}</span>
        </template>
        <!-- 循环渲染“二级菜单” -->
        <el-menu-item
          :index="'/layout' + subItem.indexPath"
          v-for="subItem in item.children"
          :key="subItem.indexPath"
        >
          <i :class="subItem.icon"></i>{{ subItem.title }}
        </el-menu-item>
      </el-submenu>
    </template>
  </el-menu>
</template>

<script>
import { getSideBarListAPI } from '@/api/sidebar'
export default {
  name: 'Sidebar',
  data() {
    return {
      // 导航菜单数据
      menus: []
    }
  },
  props: {
    showNavbar: {
      type: Boolean,
      default: false
    }
  },
  created() {
    this.getSideBarList()
  },
  methods: {
    // 获取侧边栏菜单数据
    async getSideBarList() {
      const { data: res } = await getSideBarListAPI()
      if (res.code === 0) {
        this.menus = res.data
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-aside {
  background-color: #23262e;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
  height: 100vh-6.5px;
}

.el-menu-vertical-demo {
  color: #fff;
  height: 100vh-6.5px;
}
.el-aside {
  height: 100vh-6.5px;
  overflow: hidden;
}
</style>
